package com.erp.erp_ui.Bank;

import com.erp.erp_entitys.Bank.BankEntity;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.Bank.IBankService;
import com.erp.erp_servers.OperationLog.IOperationLogService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BankControl
 * @Description 银行帐号
 * @Author songjizhen
 * @Date 2020-06-30 20:49
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "bank")
 public class BankControl extends Base {
    @Autowired
    IBankService bankService;
    @Autowired
    IOperationLogService operationLogService;

    /**
     * 新增银行帐号信息
     * @param record
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> insert(@RequestBody BankEntity record){
       try {
           int num = bankService.insertSelective(record);
           if(num>0){
               //BankEntity bankEntity = bankService.selectByPrimaryKey(Integer.parseInt(ids[0]));
               UserEntity user = (UserEntity) this.getBean("user");
               operationLogService.insertlog("新增",user.getCode(),record.getCompanyCode(),"新增了银行信息"+record.getBankname());
           }
           return R.build(num);
       }catch (Exception e){
           e.printStackTrace();
           return R.buildError(ResultStatusEnum.ERROR);
       }
    }

    /**
     *
     * @param
     * @return
     */
    @RequestMapping(value = "insert2",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> insert2(@RequestBody BankEntity record){//BankEntity record   Map<String,Object> map
        try {
           // BankEntity record=BankconverMap(map);
            int num = bankService.insertSelective(record);
            if(num>0){
               //BankEntity bankEntity = bankService.selectByPrimaryKey(Integer.parseInt(ids[0]));
                UserEntity user = (UserEntity) this.getBean("user");
                operationLogService.insertlog("新增",user.getCode(),record.getCompanyCode(),"新增了银行信息"+record.getBankname());
            }
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
private BankEntity BankconverMap(Map<String,Object> map){
    BankEntity record=new BankEntity();
  /*  record.setCompanyCode(""+map.get("companyCode"));
    record.setAddress(""+map.get("address"));
    record.setBankaccount(""+map.get("bankaccount"));
    record.setBankaccountName(""+map.get("bankaccountName"));
    record.setBankcode(""+map.get("bankcode"));
    record.setBankname(""+map.get("bankname"));
    record.setCurrency(""+map.get("currency"));
    record.setDefaultbusinessuse(Integer.parseInt(map.get("defaultbusinessuse")+""));
    record.setEnglishaddress(""+map.get("englishaddress"));
    record.setEnglishname(""+map.get("englishname"));
   record.setForBilling(""+map.get(""));
    record.setForCustomsdeclaration(""+map.get(""));
   record.setForBusiness(""+map.get(""));
    record.setStopuse(""+map.get(""));
   record.setId();*/
        return record;
}
    /**
     * 根据主键删除银行信息
     * @param
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
     public R<Integer> delete(String[] ids){
        try {
            List<Integer> list=new ArrayList<Integer>();
            for(int i=0;i<ids.length;i++){
                list.add(Integer.parseInt(ids[i].replace("[","").replace("]","")));
               // list.add(Integer.parseInt(ids[i]));
            }
           /* BankEntityExample example=new BankEntityExample();
            BankEntityExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(list);
            int num = bankService.deleteByExample(example);*/
            int num=0;

                BankEntity bankEntity = bankService.selectByPrimaryKey(list.get(0));
                UserEntity user = (UserEntity) this.getBean("user");
                operationLogService.insertlog("删除",user.getCode(),bankEntity.getCompanyCode(),"批量删除银行信息"+ids.length+"条！主键为："+ids.toString());

            for(Integer id:list){
                 num=bankService.deleteByPrimaryKey(id);
            }

            /*if(num>0){
                BankEntity bankEntity = bankService.selectByPrimaryKey(Integer.parseInt(ids[0]));
                UserEntity user = (UserEntity) this.getBean("user");
                operationLogService.insertlog("删除",user.getCode(),bankEntity.getCompanyCode(),"批量删除银行信息"+ids.length+"条！主键为："+ids.toString());
            }*/
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 查询银行信息
     * @param CompanyCode
     * @return
     */
    @RequestMapping(value = "selectByPage")
    @ResponseBody

    public R<List<BankEntity>> selectByPage(String CompanyCode){
        try {
            BankEntity record=new BankEntity();
            record.setCompanyCode(CompanyCode);
            List<BankEntity> bankEntities = bankService.selectByExample(record);
            return R.build(bankEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    @RequestMapping(value = "selectByCompanyCode")
    @ResponseBody
    public R<List<BankEntity>> selectByCompanyCode(String CompanyCode){
        try {
            BankEntity record=new BankEntity();
            record.setCompanyCode(CompanyCode);
            List<BankEntity> bankEntities = bankService.selectByExample(record);
            return R.build(bankEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 更新银行信息
     * @param record
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> update(@RequestBody BankEntity record){
        try {
            int num = bankService.updateByExampleSelective(record);
            if(num>0){
                UserEntity user = (UserEntity) this.getBean("user");
                operationLogService.insertlog("更新",user.getCode(),record.getCompanyCode(),"更新了银行信息");
            }
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }


}
